package org.example.review.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.review.dto.HrRecordDTO;
import org.example.review.dto.UserDTO;

import java.util.List;
@Mapper
public interface HrMapper extends BaseMapper<com.common.pojo.entity.user> {
    @Select("SELECT user_id, username,department,role,auth_type,password,email,create_time,is_deleted " +
            "FROM travel_user " +
            "WHERE is_deleted = 0 " +
            "ORDER BY create_time ASC")
    List<UserDTO> selectuserList();

    @Insert("INSERT INTO personnel_operation_record (user_id, operator_id, operation_type, operation_field, " +
            "old_value, new_value, operation_time, operation_ip, remark) " +
            "VALUES (#{userId}, #{operatorId}, #{operationType}, #{operationFiled}, " +
            "#{oldValue}, #{newValue}, #{operationTime}, #{operatorIp}, #{remark})")
    int insertHrRecord(HrRecordDTO hrRecord);
    @Update("UPDATE travel_user " +
            "SET username = #{username}, department = #{department}, role = #{role}, " +
            "auth_type = #{authType}, password = #{password}, email = #{email}, " +
            "is_deleted = #{isDeleted} " +
            "WHERE user_id = #{userId}")
    int updateHr(UserDTO userDTO);
    @Delete("DELETE FROM travel_user WHERE user_id = #{userId}")
    int deleteHr(UserDTO userDTO);
    @Insert("INSERT INTO travel_user (username, department, role, auth_type, password, email, create_time, is_deleted) " +
            "VALUES (#{username}, #{department}, #{role}, #{authType}, #{password}, #{email}, #{createTime}, #{isDeleted})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    int addHr(UserDTO userDTO);
    
    /**
     * 批量删除用户
     * @param userIds 用户ID列表
     * @return 删除的记录数
     */
    @Delete("<script>" +
            "DELETE FROM travel_user WHERE user_id IN " +
            "<foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteHr(List<Integer> userIds);
    
    /**
     * 批量更新用户信息（动态更新）
     * @param userIds 用户ID列表
     * @param userDTO 包含要更新的字段
     * @return 更新的记录数
     */
    @Update("<script>" +
            "UPDATE travel_user " +
            "<set>" +
            "<if test='userDTO.username != null'>username = #{userDTO.username},</if>" +
            "<if test='userDTO.department != null'>department = #{userDTO.department},</if>" +
            "<if test='userDTO.role != null'>role = #{userDTO.role},</if>" +
            "<if test='userDTO.authType != null'>auth_type = #{userDTO.authType},</if>" +
            "<if test='userDTO.email != null'>email = #{userDTO.email},</if>" +
            "<if test='userDTO.password != null'>password = #{userDTO.password},</if>" +
            "</set>" +
            "WHERE user_id IN " +
            "<foreach collection='userIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateHr(@Param("userIds") List<Integer> userIds, @Param("userDTO") UserDTO userDTO);
    
    /**
     * 查询所有人事操作记录
     * @return 操作记录列表
     */
    @Select("SELECT record_id, user_id, operator_id, operation_type, operation_field, " +
            "old_value, new_value, operation_time, operation_ip, remark " +
            "FROM personnel_operation_record " +
            "ORDER BY operation_time DESC")
    List<HrRecordDTO> selectHrRecordList();
    
    /**
     * 根据记录ID删除操作记录
     * @param recordId 记录ID
     * @return 删除的记录数
     */
    @Delete("DELETE FROM personnel_operation_record WHERE record_id = #{recordId}")
    int deleteHrRecord(Integer recordId);
    
    /**
     * 批量删除操作记录
     * @param recordIds 记录ID列表
     * @return 删除的记录数
     */
    @Delete("<script>" +
            "DELETE FROM personnel_operation_record WHERE record_id IN " +
            "<foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteHrRecords(List<Integer> recordIds);
    
    /**
     * 更新操作记录的备注
     * @param recordId 记录ID
     * @param remark 新备注
     * @return 更新的记录数
     */
    @Update("UPDATE personnel_operation_record SET remark = #{remark} WHERE record_id = #{recordId}")
    int updateHrRecordRemark(@Param("recordId") Integer recordId, @Param("remark") String remark);
}
